const koa = require("koa");
const Router = require("koa-router");
const axios = require("axios");
const cheerio = require("cheerio");
const app = new koa();
const router = new Router();
const url = "http://kuwo.cn/playlists";


router.get("/kuwo",async ctx=>{
    let html = await axios.get(url);
    const $ = cheerio.load(html.data,{
        decodeEntities:false
    });
    var result =[];
    var item = $(".rec_list .item");
    item.each((index,value)=>{
        var imgUrl = $(value).find(".pic_out img").attr("src");
        var name = $(value).find(".name span").attr("title");
        var count = $(value).find(".count").html().split("</i>")[1];
        result.push({
            imgUrl,
            name,
            count
        })
    })
    ctx.body = result;
})
// router.get("/search", async ctx => {
//     // console.log(ctx.query.keyword);
//     let {keyword} = ctx.query;
    
//     let value =encodeURIComponent(keyword);
//     console.log('value: ', value);
//     let url = `https://kuwo.cn/search/list?key=${value}`
//     let html = await axios.get(url);
//     let arr=[];
//     const $ = cheerio.load(html.data,{
//         decodeEntities:false   //解码设置为false,中文不会乱码
//     });

//     $(".flex_c").each((index,value)=>{
//         let songRank = $(value).find(".song_rank img").attr("src")
//         arr.push({
//             songRank
//         })
//         console.log(songRank);
//     })
//     // ctx.body = html;
// })

// 歌单列表 示例id：1082685104
router.get("/playlist_detail/:id", async ctx => {
    // console.log(ctx.query.keyword);
    let {id} = ctx.params;
    
    // let value =encodeURIComponent(keyword);
    console.log('id: ', id);
    let url = `https://kuwo.cn/playlist_detail/${id}`
    let html = await axios.get(url);
    let arr=[];
    const $ = cheerio.load(html.data,{
        decodeEntities:false   //解码设置为false,中文不会乱码
    });
    $(".song_item").each((index,value)=>{
        let rankNum = $(value).find(".rank_num>span:nth-child(2)").html();
        let songName = $(value).find(".song_name>.name").html();
        let songArtist = $(value).find(".song_artist>span").html();
        let songTime = $(value).find(".song_time>span").html();
        arr.push({
            rankNum,
            songArtist,
            songName,
            songTime  
        })
    })
    ctx.body = arr
})

// 歌曲详情 示例id：54993
router.get("/play_detail/:id", async ctx => {
    // console.log(ctx.query.keyword);
    let {id} = ctx.params;
    
    // let value =encodeURIComponent(keyword);
    console.log('id: ', id);
    let url = `https://kuwo.cn/play_detail/${id}`
    let html = await axios.get(url);
    let arr=[];
    const $ = cheerio.load(html.data,{
        decodeEntities:false   //解码设置为false,中文不会乱码
    });
    let songName = $(".song_name").find(".name").html().trim().split("<")[0];
    let artistName = $(".artist_name").find(".name").html().trim();
    let intrTxt = $(".intr_txt").html().trim().split("<")[0];
    arr.push({
        songName,
        artistName,
        intrTxt
    })
    ctx.body = arr;
})
app.use(router.routes());
app.listen(8080);